﻿using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using System.Text.Json;
using System.IO;

namespace SerialProxy
{
    [SupportedOSPlatform("windows")]
    [SupportedOSPlatform("linux")]
    class Program
    {
        static void Main(string[] args)
        {
            var proxy = new List<ProxyLib.SerialProxy>();
            try
            {
                using (var cfg = new StreamReader("Resources/port.txt"))
                {
                    var json = cfg.ReadToEnd();
                    var cfg_json = JsonSerializer.Deserialize<PortConfig>(json);
                    foreach (var p in cfg_json.portLists)
                    {
                        proxy.Add(new ProxyLib.SerialProxy(p.tcpPort, p.name, p.baudrate));
                    }
                }
            }
            catch (Exception ex)
            {
                var c = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = c;
            }
            foreach (var p in proxy)
                p.Begin();
            Console.ReadKey();
        }
    }

    public class PortConfig
    {
        public class Port
        {
            public string name { get; set; }
            public uint baudrate { get; set; }
            public uint tcpPort { get; set; } 
        }
        public List<Port> portLists { get; set; } = new();
    }
}
